package c.x.jy.admin.controller;

import java.util.Arrays;
import java.util.Map;

import c.x.jy.dao.entity.CmsArticleDataEntity;
import c.x.jy.dao.entity.CmsArticleEntity;
import c.x.jy.service.service.CmsArticleDataService;
import c.x.jy.service.service.CmsArticleService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


import c.x.jy.common.utils.PageUtils;
import c.x.jy.common.utils.R;



/**
 * 文章正文
 *
 * @author admin
 * @email 
 * @date 2020-09-17 12:20:32
 */
@RestController
@RequestMapping("cms/cmsarticledata")
public class CmsArticleDataController  extends AbstractController{
    @Autowired
    private CmsArticleDataService cmsArticleDataService;
    
    @Autowired
    private CmsArticleService cmsArticleService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("cms:cmsarticledata:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = cmsArticleDataService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("cms:cmsarticledata:info")
    public R info(@PathVariable("id") Integer id){
    	if(id==null)R.error();
		CmsArticleDataEntity cmsArticleData = cmsArticleDataService.getById(id);

        return R .put(   cmsArticleData);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
   @RequiresPermissions("cms:cmsarticledata:save")
    public R save(@RequestBody CmsArticleDataEntity cmsArticleData){
    	boolean i;
    	if(cmsArticleData.getTitleId()==null)R.error("保存失败！");
    	if(cmsArticleData.getId()==null) {
            cmsArticleData.setBusinessId(getUser().getBusinessId());
	    i=	cmsArticleDataService.save(cmsArticleData);
	    CmsArticleEntity entity=new CmsArticleEntity();
	    entity.setId(cmsArticleData.getTitleId());
	    entity.setArticleDataId(cmsArticleData.getId());
		cmsArticleService.updateById(entity);
    	}else {
    		  i=	cmsArticleDataService.updateById(cmsArticleData);
    	}
        return i?R.ok():R.error("保存失败！");
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("cms:cmsarticledata:update")
    public R update(@RequestBody CmsArticleDataEntity cmsArticleData){
		cmsArticleDataService.updateById(cmsArticleData);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("cms:cmsarticledata:delete")
    public R delete(@RequestBody Integer[] ids){
		cmsArticleDataService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
